package com.inspur.gs.sbssdl.gzgl.gzlx.gzlx.front.gzlxspxxcx.voactions;
import com.inspur.edp.bff.api.manager.context.QueryContext;
import com.inspur.edp.bff.spi.action.query.AbstractQueryAction;
import com.inspur.edp.caf.db.dbaccess.DbParameter;
import com.inspur.edp.caf.db.dbaccess.DynamicResultRow;
import com.inspur.edp.caf.db.dbaccess.IDbParameter;
import com.inspur.edp.cef.entity.condition.EntityFilter;
import com.inspur.edp.cef.entity.condition.FilterCondition;
import com.inspur.edp.cef.entity.entity.IEntityData;
import com.inspur.edp.qdp.bql.api.IBqlExecuter;
import com.inspur.edp.qdp.bql.api.OptionType;
import io.iec.edp.caf.runtime.config.BqlBeanUtil;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

public class gzlxsqSearchVOAction extends AbstractQueryAction {

	public gzlxsqSearchVOAction(QueryContext context){
		super(context);
	}


	private final IBqlExecuter bqlExecuter = BqlBeanUtil.getAppCtx().getBean(IBqlExecuter.class);

	@Override
	public void execute() {
		ArrayList<String> beIds = new ArrayList<>();
		beIds.add("fdb635c5-540f-4b7a-ba4c-9adf614f00af");
		String gzlxsqsql = "SELECT gg.sqdw_name,gg.lsh,ggm.spdm,ggm.kygh,ggm.gcbh,gg.xqrq,ggm.gzxxid_ysdm " +
				",ggm.gzxxid_name,ggm.ggxh,ggm.gzxxid_dw,ggm.sqsl,ggm.wlbh,gg.bz,ggm.spsl,gg.sqrq,gg.cljg,gg.BillStatus,ggm.gzxxid_lb " +
				" FROM gzlxsq.gzlxsq gg LEFT JOIN gzlxsq.gzlxsqmx ggm ON gg.ID = ggm.PARENTID WHERE 1=1 ";
		//申明变量
		String sqdw;
//		String xqrq;
		String xqrqBegin;
		String xqrqEnd;
		String gcbh;
		String zt;
		String spdm;
		String bz;
		String ysdm;
		String name;
		String cljg;
		String ggxh;
		String lb;

		Map<String, Object> bqlParams = new LinkedHashMap<>();
		EntityFilter filter = getQueryContext().getFilter();
		List<String> xqrqList = filter.getFilterConditions().stream()
				.filter(s -> s.getFilterField().equals("xqrq"))
				.map(s -> s.getValue())  // 假设你要取的是 getValue() 的值
				.collect(Collectors.toList());

		if (!xqrqList.isEmpty() && xqrqList.size() >= 2) {
			String date1 = xqrqList.get(0);
			String date2 = xqrqList.get(1);
			if (date1.compareTo(date2) > 0) {
				// 如果 date1 > date2，交换它们
				String temp = date1;
				date1 = date2;
				date2 = temp;
			}
			gzlxsqsql += " AND gg.xqrq BETWEEN TO_TIMESTAMP(@xqrqBegin, 'YYYY-MM-DD HH24:MI:SS') " +
					"AND TO_TIMESTAMP(@xqrqEnd, 'YYYY-MM-DD HH24:MI:SS')";
			bqlParams.put("xqrqBegin", date1 + " 00:00:00");  // 补充时间部分
			bqlParams.put("xqrqEnd", date2 + " 23:59:59");    // 补充时间部分
		}


		//获取筛选条件
		for(int i=0;i<filter.getFilterConditions().size();i++){
			if(filter.getFilterConditions().get(i).getFilterField().equals("sqdw")){
				sqdw = filter.getFilterConditions().get(i).getValue();
				gzlxsqsql +=" and gg.sqdw_name = @sqdw";
				bqlParams.put("sqdw", sqdw);
			}else if(filter.getFilterConditions().get(i).getFilterField().equals("gcbh")){
				gcbh = filter.getFilterConditions().get(i).getValue();
				gzlxsqsql += " and ggm.gcbh = @gcbh";
				bqlParams.put("gcbh", gcbh);
			}else if(filter.getFilterConditions().get(i).getFilterField().equals("zt")){
				zt = filter.getFilterConditions().get(i).getValue();
				gzlxsqsql += " and gg.BillStatus = @zt";
				bqlParams.put("zt", zt);
			}else if(filter.getFilterConditions().get(i).getFilterField().equals("spdm")){
				spdm = filter.getFilterConditions().get(i).getValue();
				gzlxsqsql += " and ggm.spdm = @spdm";
				bqlParams.put("spdm", spdm);
			}else if(filter.getFilterConditions().get(i).getFilterField().equals("bz")){
				bz = filter.getFilterConditions().get(i).getValue();
				gzlxsqsql += " and gg.bz = @bz";
				bqlParams.put("bz", bz);
			}else if(filter.getFilterConditions().get(i).getFilterField().equals("ysdm")){
				ysdm = filter.getFilterConditions().get(i).getValue();
				gzlxsqsql += " and ggm.gzxxid_ysdm = @ysdm";
				bqlParams.put("ysdm", ysdm);
			}else if(filter.getFilterConditions().get(i).getFilterField().equals("name")){
				name = filter.getFilterConditions().get(i).getValue();
				gzlxsqsql += " and ggm.gzxxid_name = @name";
				bqlParams.put("name", name);
			}else if(filter.getFilterConditions().get(i).getFilterField().equals("cljg")){
				cljg = filter.getFilterConditions().get(i).getValue();
				gzlxsqsql += " and gg.cljg = @cljg";
				bqlParams.put("cljg", cljg);
			}else if(filter.getFilterConditions().get(i).getFilterField().equals("ggxh")){
				ggxh = filter.getFilterConditions().get(i).getValue();
				gzlxsqsql += " and ggm.ggxh = @ggxh";
				bqlParams.put("ggxh", ggxh);
			}else if(filter.getFilterConditions().get(i).getFilterField().equals("lb")){
				lb = filter.getFilterConditions().get(i).getValue();
				gzlxsqsql += " and ggm.gzxxid_lb = @lb";
				bqlParams.put("lb", lb);
			}
		}
		IDbParameter[] params = new DbParameter[bqlParams.size()];
		int paramCount = 0;
		for (Map.Entry<String, Object> bqlParam : bqlParams.entrySet()) {
			params[paramCount++] = bqlExecuter.makeInParam(bqlParam.getKey(), bqlParam.getValue());
		}
		bqlExecuter.getOptions().setOptionType(OptionType.ExecuteBql);
		List<DynamicResultRow> rows = bqlExecuter.executeSelectStatement(gzlxsqsql, beIds,params);
		// 结果取数
		ArrayList<IEntityData> entityData=new ArrayList<>();
		for(DynamicResultRow resultRow:rows){
			IEntityData voEntityData =getContext().createData();
			String uuid = UUID.randomUUID().toString();
			voEntityData.setValue("ID",uuid);
			if(resultRow.getValues().get(0) != null) voEntityData.setValue("sqdw",resultRow.getValues().get(0).toString());
			if(resultRow.getValues().get(1) != null) voEntityData.setValue("lsh",resultRow.getValues().get(1).toString());
			if(resultRow.getValues().get(2) != null) voEntityData.setValue("sqdm",resultRow.getValues().get(2).toString());
			if(resultRow.getValues().get(3) != null) voEntityData.setValue("kybh",resultRow.getValues().get(3).toString());
			if(resultRow.getValues().get(4) != null) voEntityData.setValue("gcbh",resultRow.getValues().get(4).toString());
			if (resultRow.getValues().get(5) != null) {
				Object value = resultRow.getValues().get(5);
				if (value instanceof Timestamp) {
					// 转换为 java.util.Date
					voEntityData.setValue("xqrq", new java.util.Date(((Timestamp) value).getTime()));
				} else {
					// 如果是 String，尝试解析成 Date（如 "2025-08-21 11:06:39.0"）
					try {
						SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");
						Date parsedDate = sdf.parse(value.toString());
						voEntityData.setValue("xqrq", parsedDate);
					} catch (Exception e) {
						throw new RuntimeException("无法解析日期: " + value, e);
					}
				}
			}
			if(resultRow.getValues().get(6) != null) voEntityData.setValue("ysdm",resultRow.getValues().get(6).toString());
			if(resultRow.getValues().get(7) != null) voEntityData.setValue("name",resultRow.getValues().get(7).toString());
			if(resultRow.getValues().get(8) != null) voEntityData.setValue("ggxh",resultRow.getValues().get(8).toString());
			if(resultRow.getValues().get(9) != null) voEntityData.setValue("dw",resultRow.getValues().get(9).toString());
			if(resultRow.getValues().get(10) != null) voEntityData.setValue("sqsl",((BigDecimal)resultRow.getValues().get(10)).intValue());
			if(resultRow.getValues().get(11) != null) voEntityData.setValue("wlbh",resultRow.getValues().get(11).toString());
			if(resultRow.getValues().get(12) != null) voEntityData.setValue("bz",resultRow.getValues().get(12).toString());
			if(resultRow.getValues().get(13) != null) voEntityData.setValue("spsl",((BigDecimal)resultRow.getValues().get(13)).intValue());
			if(resultRow.getValues().get(14) != null) voEntityData.setValue("sqrq",resultRow.getValues().get(14));
			if(resultRow.getValues().get(15) != null) voEntityData.setValue("cljg",resultRow.getValues().get(15).toString());
			if(resultRow.getValues().get(16) != null) voEntityData.setValue("zt",resultRow.getValues().get(16).toString());
			if(resultRow.getValues().get(17) != null) voEntityData.setValue("lb",resultRow.getValues().get(17).toString());
			entityData.add(voEntityData);
		}
		HashMap<String,ArrayList<IEntityData>> queryResult = new HashMap<String,ArrayList<IEntityData>>();
		queryResult.put("queryresult",entityData);
		setResult(queryResult);

	}
}
